import 'package:flutter/material.dart';

///
/// 邀请好友
///
class QInvitationSheet extends Dialog {
  final BuildContext buildContext;
  final List dataList;
  final Function? onTap;

  const QInvitationSheet({
    Key? key,
    required this.buildContext,
    required this.dataList,
    this.onTap,
  }) : super(key: key);

  /// 取消对话框
  static hide(BuildContext context) {
    Navigator.of(context).pop();
  }

  /// 显示对话框
  void show() {
    // 用于在底部打开弹框的效果
    showModalBottomSheet(
      backgroundColor: Colors.transparent, // 重要
      context: buildContext,
      builder: (BuildContext context) {
        // 构建弹框中的内容
        return InvitationFriendsContentView(
          dataList: dataList,
          tapCallback: onTap,
        );
      },
    );
  }
}

class InvitationFriendsContentView extends StatefulWidget {
  final List dataList;
  final Function? tapCallback;

  const InvitationFriendsContentView({
    Key? key,
    required this.dataList,
    this.tapCallback,
  }) : super(key: key);

  @override
  _InvitationFriendsContentViewState createState() =>
      _InvitationFriendsContentViewState();
}

class _InvitationFriendsContentViewState
    extends State<InvitationFriendsContentView> {
  @override
  Widget build(BuildContext context) {
    return buildBottomSheetWidget(context);
  }

  ///底部弹出框的内容
  Widget buildBottomSheetWidget(BuildContext context) {
    return Container(
      height: 650 / 2,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(25.0),
          topRight: Radius.circular(25.0),
        ),
      ),
      child: Column(
        children: [
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
            child: Text(
              "邀请好友",
              style: TextStyle(
                color: Color(0xFF36393D),
                fontSize: 18,
              ),
            ),
          ),
          horizontalListWidget(showText: false),
          const SizedBox(height: 16),
          commonButtonWidget(fontSize: 16, txt: "邀请", onTap: () {}),
          Container(
            width: MediaQuery.of(context).size.width,
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: const Text("去邀请"),
          ),
          horizontalListWidget()
        ],
      ),
    );
  }

  Widget commonButtonWidget(
      {String? txt,
      Color? fontColor,
      Color? backColor,
      double? fontSize,
      Function? onTap}) {
    String txt = "邀请";
    Color fontColor = const Color(0xFF19D88E);
    Color backColor = const Color(0xFF19D88E).withOpacity(0.2);
    double fontSize = 18;
    return Center(
      child: Material(
        // INK可以实现装饰容器
        child: Ink(
          // 用ink圆角矩形
          decoration: BoxDecoration(
            // 不能同时”使用Ink的变量color属性以及decoration属性，两个只能存在一个
            color: backColor,
            // 设置圆角
            borderRadius: const BorderRadius.all(Radius.circular(25.0)),
          ),
          child: InkWell(
            // 圆角设置,给水波纹也设置同样的圆角
            // 如果这里不设置就会出现矩形的水波纹效果
            borderRadius: BorderRadius.circular(25.0),
            // 设置点击事件回调
            onTap: () {
              onTap!();
            },
            child: Container(
              width: 300.0,
              height: 36.0,
              // 设置child 居中
              alignment: const Alignment(0, 0),
              child: Text(
                txt,
                style: TextStyle(color: fontColor, fontSize: fontSize),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget horizontalListWidget({bool showText = true}) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      height: showText ? 75 : 50,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: widget.dataList.length,
        itemBuilder: (BuildContext context, int index) {
          UserInfoModel e = widget.dataList[index];
          return InkWell(
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 6),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0.0, 3, 0.0, 3),
                    child: Image.asset(
                      e.url,
                      width: 44,
                    ),
                  ),
                  showText ? Text(e.name) : Container()
                ],
              ),
            ),
            onTap: () {
              switch (e.name) {
                case '微信':
                  debugPrint("微信");
                  break;
                case '朋友圈':
                  debugPrint("朋友圈");
                  break;
                case 'QQ':
                  debugPrint("QQ");
                  break;
                case 'QQ空间':
                  debugPrint("QQ空间");
                  break;
                case '微博':
                  debugPrint("微博");
                  break;
              }
            },
          );
        },
      ),
    );
  }
}

/// 用户的信息
class UserInfoModel {
  final String name;
  final String url;

  UserInfoModel({required this.name, required this.url});

  @override
  String toString() {
    // TODO: implement toString
    return "{name:$name, url:$url}";
  }
}
